//
//  MYGMedicalToolsHasController.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/4/21.
//

import UIKit

class MYGMedicalToolsHasController: MYGBaseController {
    // MARK: - Property
    private var H: Int = -1
    private var A: Int = -1
    private var S: Int = -1
    private var B: Int = -1
    private var L: Int = -1
    private var E: Int = -1
    private var D: Int = -1

    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    // MARK: - UI
    private func configUI() {
        view.backgroundColor = .white
        view.addSubviews([
            scrollView,
            commitButton
        ])
        
        scrollView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(commitButton.snp.top)
        }
        
        commitButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(49)
            make.bottom.equalToSuperview().offset(-bottomSafeheight)
        }
        
        scrollView.addSubview(scrollContentView)
        scrollContentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalToSuperview()
        }
        
        let HView = createChooseView(title: "高血压", index: 0)
        let AView = createChooseView(title: "肾或肾功能异常", index: 1)
        let SView = createChooseView(title: "脑血管意外史", index: 2)
        let BView = createChooseView(title: "出血史", index: 3)
        let LView = createChooseView(title: "INR不稳定", index: 4)
        let EView = createChooseView(title: "老年（>65岁）", index: 5)
        let DView = createChooseView(title: "服药或酗酒", index: 6)

        scrollContentView.addSubviews([
            HView,
            AView,
            SView,
            BView,
            LView,
            EView,
            DView,
            scoreView
        ])
        
        
        HView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        AView.snp.makeConstraints { make in
            make.top.equalTo(HView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        SView.snp.makeConstraints { make in
            make.top.equalTo(AView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        BView.snp.makeConstraints { make in
            make.top.equalTo(SView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        LView.snp.makeConstraints { make in
            make.top.equalTo(BView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        EView.snp.makeConstraints { make in
            make.top.equalTo(LView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        DView.snp.makeConstraints { make in
            make.top.equalTo(EView.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview().inset(26)
        }
        
        scoreView.snp.makeConstraints { make in
            make.top.equalTo(DView.snp.bottom)
            make.height.equalTo(108)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    func createSeparator() -> UIView {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238)
        return view
    }
    
    
    func createChooseView(title: String, index:Int) -> MYGMedicalToolsChooseView {
        let view = MYGMedicalToolsChooseView(frame: .zero)
        view.title = title
        view.leftTitle = "是"
        view.rightTitle = "否"
        view.selectedAction = {(ok) in
            if index == 0 {
                self.H = ok
            } else if index == 1 {
                self.A = ok
            } else if index == 2 {
                self.S = ok
            } else if index == 3 {
                self.B = ok
            } else if index == 4 {
                self.L = ok
            } else if index == 5 {
                self.E = ok
            } else if index == 6 {
                self.D = ok
            }
        }
        
        let separator = createSeparator()
        view.addSubview(separator)
        separator.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        return view
    }
    
    // MARK: - lazy load
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()

    private lazy var scrollContentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
        
    private lazy var scoreView: MYGMedicalToolsScoreView = {
        let view = MYGMedicalToolsScoreView()
        view.level = "正常"
        view.title = "0"
        return view
    }()
    
    private lazy var commitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .rgba(151, 179, 255)
        button.setTitle("开始计算", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = .medium(17)
        button.addTarget(self, action: #selector(commit), for: .touchUpInside)
        return button
    }()
}

extension MYGMedicalToolsHasController {
    @objc func commit() {
        view.endEditing(true)
        guard H != -1 && A != -1 && S != -1 && B != -1 && L != -1 && E != -1 && D != -1 else { return }
        
        var score = ScoreValue(H).score + ScoreValue(A).score + ScoreValue(S).score + ScoreValue(B).score + ScoreValue(L).score + ScoreValue(E).score + ScoreValue(D).score
        if score < 3 {
            scoreView.level = "正常"
        } else {
            scoreView.level = "高危"
        }
        scoreView.title = "\(score)"
        scoreView.progress = Double(score) / 7.0
    }
}

@propertyWrapper
final class ScoreValue {
    private var lock: NSRecursiveLock = NSRecursiveLock()

    private var value: Swift.Int
    var score: Swift.Int {
        if value == 0 {
            return 1
        }
        return 0
    }
    var wrappedValue: Swift.Int {
        get {
            lock.lock(); defer { lock.unlock() }
            return value
        }

        set {
            lock.lock(); defer { lock.unlock() }
            value = newValue
        }
    }

    init(_ value: Swift.Int) {
        self.value = value
    }
}
